﻿using Dapper;

using HBase;
using hCommand;
using System;
using System.Collections.Generic;
using System.Linq;
using static Dapper.SqlMapper;

namespace Persistence {
    public class AbsDoServer : AbsExtendsServer, IDoServer
    { 

        public List<T> getDetials<T>(long pid) where T : BaseEntity { 
            return readConn().GetAll<T>(" and pid=@pid", new { pid }) ;
        }
        public void finshDo<T>(T dto) where T : AbsDo {
            dto.status = 9;
            dto.finshTime= DateTime.Now;
            opConn().Update(dto, x=>new { x.status, x.finshTime } );
        }

        public double getLastRamain(WhDoStat stat){
            String sql = "select remain from Wh_Do_Stat where item_Id=@itemId AND batch_No=@batchNo   order by id desc  LIMIT 1";
            double c = readConn().QueryUnique<double>(sql, stat);

            return c;
        }
        public void batchInsert(List<WhDoStat> list) {

            opConn().BatchInsert(list);   
        }

        public void statData() {

            String sql = "SELECT sum(s.count_db) in_Count,s.item_Id  FROM `core_stock_det` s join wh_doi_det i on s.doi_det_id=i.id   where s.pid>0 and s.create_Time>CURDATE()   GROUP BY s.item_Id";

            List<WhDoStat> inStat= opConn().Query<WhDoStat>(sql).ToList();
            String outSql = "SELECT sum(s.count_db) out_Count,s.item_Id  FROM `core_stock_det` s join wh_doo_det o on s.doo_det_id=o.id   where s.status<0 and s.out_Time>CURDATE() GROUP BY s.item_Id";

            List<WhDoStat> outStat = opConn().Query<WhDoStat>(outSql).ToList();
            foreach (WhDoStat os in outStat) {
                checkOut(os, inStat);
            }
            for (int i = 0; i < inStat.Count; i++) {
                 inStat[i].createTime= DateTime.Now;    
            }
            opConn().Execute("delete  from Wh_Do_Stat where create_Time>= CURDATE()");

            if (inStat.Count > 0) {
                opConn().BatchInsert(inStat);
                String update = "update Wh_Do_Stat s set remain=(select IFNULL(count,0)  from core_item WHERE id=s.item_id) where  create_Time>= CURDATE()";
                opConn().Execute(update);

            }

        }



        void  checkOut(WhDoStat os, List<WhDoStat> inStat) {
            for (int i = 0; i < inStat.Count; i++) {
                if (inStat[i].itemId == os.itemId) {
                    inStat[i].outCount = os.outCount;
                    return;
                }
            }
            inStat.Add(os); 
        }

        public Page<WhDoStat> queryStat(PDoStatDto dto) {
            string sql = @" SELECT  B.* FROM    Wh_Do_Stat B   where 1=1  ";

            if ( dto.itemId>0) {
                sql += "and item_Id=@itemId ";
            }
            if (!String.IsNullOrEmpty(dto.batchNo)) {
                dto.batchNo = "%" + dto.batchNo + "%";
                sql += "and batch_No like @batchNo ";
            }

            if (dto.b.Ticks > 0) {
                sql += "and  create_Time > @b ";
            }
            if (dto.e.Ticks > 0) {
                sql += "and  create_Time < @e ";
            }

            return queryPage<WhDoStat>(sql, " create_time  desc", dto);
        }
        public List<WhDoStat> getStat(PDoStatDto dto) {

            String where = " where 1=1 ";
            if (dto.itemId > 0) {
                where += " and item_Id=@itemId ";
            }
            if (dto.b.Ticks > 0) {
                where += " and  create_Time >= @b ";
            }
            if (dto.e.Ticks > 0) {
                dto.e=dto.e.AddDays(1);   
                where += " and  create_Time <= @e ";
            }


            string sql = $"select * from Wh_Do_Stat {where} ";
        

                return readConn().Query<WhDoStat>(sql, dto).ToList();
           
    

        }

    }
}
